package tom.client.admin;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.shared.Program;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.user.client.ui.CheckBox;

public class FormAppForm extends DataForm {
	@Override
	public void setTitle(String title) {
		super.setTitle(title);
		Library.getAdminService().selectModule(moduleCallback);
	}

	public FormAppForm() {
		lbCode.setText("รหัสฟอร์ม");

		textFormName = new TextBox();
		textFormName.setMaxLength(32);
		add(textFormName, 122, 121);
		textFormName.setSize("150px", "18px");

		textDescript = new TextBox();
		textDescript.setMaxLength(150);
		add(textDescript, 122, 153);
		textDescript.setSize("366px", "18px");

		textModule = new TextBox();
		textModule.addFocusHandler(new FocusHandler() {
			public void onFocus(FocusEvent event) {
				listBox.setVisible(true);
			}
		});
		add(textModule, 122, 185);
		textModule.setSize("100px", "18px");

		Label lblNewLabel = new Label("ชื่อฟอร์ม");
		add(lblNewLabel, 21, 121);

		Label lblNewLabel_1 = new Label("คำอธิบาย");
		add(lblNewLabel_1, 21, 153);

		Label lblNewLabel_2 = new Label("ระบบ");
		add(lblNewLabel_2, 21, 185);

		listBox = new ListBox();
		listBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				listBox.setVisible(false);
				textModule.setText(listBox.getItemText(listBox
						.getSelectedIndex()));
			}
		});
		add(listBox, 122, 211);
		listBox.setSize("127px", "86px");
		listBox.setVisibleItemCount(10);

		chkActive = new CheckBox("เปิดการทำงาน");
		add(chkActive, 508, 121);
		chkActive.setSize("132px", "26px");
		this.setFieldsHandler();
	}

	private static FormAppForm panel;

	public static FormAppForm getPanel() {
		if (panel == null) {
			panel = GWT.create(FormAppForm.class);
		}
		return panel;
	}

	@Override
	protected void search() {
		LookupDialog.show("ฟอร์ม", "pgmId", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getAdminService().selectForm(txtCode.getText(), formCallback);
		Library.getAdminService().selectModule(moduleCallback);
	}

	private Program record;

	private AsyncCallback<tom.shared.Program> formCallback = new AsyncCallback<tom.shared.Program>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(Program result) {
			record = result;
			setFields();
		}
	};

	private AsyncCallback<List<String>> moduleCallback = new AsyncCallback<List<String>>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(List<String> result) {
			listBox.clear();
			for (String modName : result)
				listBox.addItem(modName);
		}
	};

	private final TextBox textFormName;

	private final TextBox textDescript;

	private final TextBox textModule;

	private final ListBox listBox;

	private final CheckBox chkActive;

	@Override
	protected void update() {
		getFields();
		Library.getAdminService().updateForm(record, delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		Library.getAdminService().insertForm(record, delCallback);
	}

	@Override
	protected void delete() {
		if (Window.confirm("ต้องการลบโปรแกรม " + textFormName.getText()
				+ " หรือไม่")) {
			Library.getAdminService()
					.deleteForm(txtCode.getText(), delCallback);
		}

	}

	@Override
	protected void clearFields() {
		super.clearFields();
		textFormName.setText(null);
		textDescript.setText(null);
		textModule.setText(null);
		chkActive.setValue(null);
		listBox.setVisible(false);
	}

	@Override
	protected void setFields() {
		if (record != null) {
			super.setFields();
			txtCode.setText(record.pgmId);
			textFormName.setText(record.pgmName);
			textDescript.setText(record.descript);
			textModule.setText(record.module);
			chkActive.setValue(record.active);
		} else {
			String code = txtCode.getText();
			clearFields();
			super.setMode(3);
			txtCode.setText(code);
		}
	}

	@Override
	protected void getFields() {
		if (record == null)
			record = new Program();
		record.pgmId = txtCode.getText();
		record.pgmName = textFormName.getText();
		record.descript = textDescript.getText();
		record.module = textModule.getText();
		record.active = chkActive.getValue();
	}

	@Override
	protected String getReportFile() {
		return "report002.jasper";
	}

	@Override
	protected Map<String, Object> genReportParams() {
		return null;
	}

	@Override
	protected void setFieldsHandler() {
		super.setFieldsHandler();
		chkActive.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (txtCode.getText() != null
						&& txtCode.getText().equals("004form")) {
					Window.alert("แก้ไขการเปิด/ปิดการทำงานของ "
							+ txtCode.getText() + " ไม่ได้");
					chkActive.setValue(true);
				}
				changeHandler.onChange(null);
			}
		});
	}
}
